feat(cli): add "testsprite completion" for bash/zsh/fish#227
Conversation
Emit a shell completion script for bash, zsh, or fish. Command names, subcommands, and global flags are derived from the fully-assembled Commander tree at call time (buildCompletionSpec walks program.commands), so the script can never drift from the real command surface. The shell auto-detects from $SHELL when the argument is omitted. Fixes TestSprite#74
WalkthroughThis PR adds a ChangesShell Completion Command
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant createCompletionCommand
participant renderCompletion
User->>CLI: testsprite completion [shell]
CLI->>createCompletionCommand: invoke with args
createCompletionCommand->>createCompletionCommand: resolve shell (arg or detectShell(env))
alt shell missing or unsupported
createCompletionCommand-->>CLI: throw VALIDATION_ERROR
else shell resolved
createCompletionCommand->>renderCompletion: renderCompletion(shell, getSpec())
renderCompletion-->>createCompletionCommand: script string
createCompletionCommand->>CLI: write script to stdout
end
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/commands/completion.test.ts (2)
75-75: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider asserting
stdoutis never invoked on validation-error paths.Both error-path tests pass
stdout: () => undefined, discarding any calls. Asserting that stdout was not called before the rejection (e.g. via a spy) would strengthen the "machine output only goes to stdout on success" guarantee called out in the path instructions' output-discipline rule.Also applies to: 82-82
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/commands/completion.test.ts` at line 75, Strengthen the validation-error tests in createCompletionCommand by asserting that the provided stdout handler is never called on rejection paths, rather than using a no-op function. Update the error-path cases in completion.test.ts to use a spy/mock for stdout and verify it has zero calls when the command fails validation, keeping the success-path behavior in createCompletionCommand and stdout output-discipline explicitly covered.Source: Path instructions
74-86: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winTest titles claim exit code 5 but only assert the
ApiError.codefield, not the numeric exit code.Both tests title-comment "(exit 5)" but only check
code: 'VALIDATION_ERROR'on the rejected error. The path instructions require that error paths map to the documented exit code (5 for validation errors). Since this test never asserts the numeric exit code, the actual mapping fromVALIDATION_ERROR→ exit code5is unverified at this layer — a regression in the exit-code-mapping utility (e.g., insrc/index.tsor an error-handling layer) wouldn't be caught here.Consider either renaming the test titles to drop the misleading "(exit 5)" claim, or asserting the mapping directly if a helper (e.g.
codeToExitCode) is exposed/testable.As per path instructions, "Exit-code mapping: error paths must map to the documented exit code (see DOCUMENTATION.md / the exit-code table); don't introduce ad-hoc codes."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/commands/completion.test.ts` around lines 74 - 86, The completion tests currently mention “exit 5” in the titles but only assert ApiError.code, so they do not verify the documented validation exit code mapping. Update the tests in completion.test to either remove the exit-code claim from the titles or add an assertion against the numeric exit-code mapping for VALIDATION_ERROR using the relevant helper or error-handling path. Use the existing createCompletionCommand and parseAsync cases to keep the checks aligned with the command’s validation failure behavior.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/commands/completion.test.ts`:
- Line 75: Strengthen the validation-error tests in createCompletionCommand by
asserting that the provided stdout handler is never called on rejection paths,
rather than using a no-op function. Update the error-path cases in
completion.test.ts to use a spy/mock for stdout and verify it has zero calls
when the command fails validation, keeping the success-path behavior in
createCompletionCommand and stdout output-discipline explicitly covered.
- Around line 74-86: The completion tests currently mention “exit 5” in the
titles but only assert ApiError.code, so they do not verify the documented
validation exit code mapping. Update the tests in completion.test to either
remove the exit-code claim from the titles or add an assertion against the
numeric exit-code mapping for VALIDATION_ERROR using the relevant helper or
error-handling path. Use the existing createCompletionCommand and parseAsync
cases to keep the checks aligned with the command’s validation failure behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 3bdcef1b-59de-4a11-b63e-d4de4eade15b
⛔ Files ignored due to path filters (1)
test/__snapshots__/help.snapshot.test.ts.snapis excluded by!**/*.snap,!**/*.snap
📒 Files selected for processing (3)
src/commands/completion.test.tssrc/commands/completion.tssrc/index.ts
Adds
testsprite completion [bash|zsh|fish]: prints a shell completion script.Command names, per-group subcommands, and global flags are NOT hardcoded:
index.ts walks the fully-assembled Commander program (buildCompletionSpec) and
passes a CompletionSpec in, so the generated script can never drift from the
real command tree. renderCompletion is a pure function of the spec
(unit-testable without a live program). The shell auto-detects from $SHELL
when omitted.
Enable it:
Fixes #74
Summary by CodeRabbit
New Features
completioncommand to generate shell completion scripts.bash,zsh, andfish, with automatic shell detection when possible.Bug Fixes
Tests